package simorg.actions;

import simorg.controller.CourseController;
import simorg.domain.Course;
import simorg.domain.persistence.CourseTable;
import simorg.view.ModelView;
import simorg.view.SessionAttribute;
import simorg.view.WebSessionContext;

public class CourseSave extends Action {

	public static String actionName = "save";
	public CourseSave() {
		super(actionName);
		this.controllerName = CourseController.controllerName;
		
	}
	
	@Override
	public ModelView checkAccess(WebSessionContext wc) throws Exception {
		return this.checkAdminAccess(wc);
	}

	@Override
	public ModelView process(WebSessionContext wc) throws Exception {
		
		
		Course courseToSave = CourseTable.findById(wc.getDbConnection(), Integer.parseInt(this.getRequestArgs().get(0)));
		
		// Security
		
		// Process post params
		if ( wc.hasReqeustParameter("name")) {
			courseToSave.setName(wc.getRequestParameter("name"));
		}
		if ( wc.hasReqeustParameter("semester")) {
			courseToSave.setSemester(wc.getRequestParameter("semester"));
		}
		if ( wc.hasReqeustParameter("section")) {
			courseToSave.setSection(wc.getRequestParameter("section"));
		}
		if ( wc.hasReqeustParameter("instructor")) {
			courseToSave.setInstructorID(Integer.parseInt(wc.getRequestParameter("instructor")));
		}
		
		// Save the user infromation
		CourseTable.save(wc.getDbConnection(), courseToSave);
		
		// Redirect to user default.
		wc.redirectAdmin((Action)wc.getSessionAttribute(SessionAttribute.DefaultAction));
		return null;
	}
}
